

//
//  AuthLoginViewController.m
//  Luxury
//
//  Created by DCQ on 2018/5/18.
//  Copyright © 2018年 ZNXZ. All rights reserved.
//

#import "AuthLoginViewController.h"
#import "UserInfoModel.h"
 
@interface AuthLoginViewController ()
@property (weak, nonatomic) IBOutlet UIView *accountBgV;
@property (weak, nonatomic) IBOutlet UIView *passwordBgV;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (weak, nonatomic) IBOutlet UITextField *accountTF;
@property (weak, nonatomic) IBOutlet UIButton *verificationCodeBtn;
@property (weak, nonatomic) IBOutlet UITextField *passwordTF;
@property (weak, nonatomic) IBOutlet UIButton *smsButton;
@property (nonatomic) NSTimer *sendTimer;
@property (nonatomic) NSDate  *fireDate;

@end

@implementation AuthLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configView];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.sendTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(sendSms) userInfo:nil repeats:YES];
}
- (void)configView{
    self.accountBgV.layer.masksToBounds = YES;
    self.accountBgV.layer.borderWidth = 1;
    self.accountBgV.layer.borderColor =  UIColorFromRGB(0xE0E0E0).CGColor;
    self.accountBgV.layer.cornerRadius = 40/2;
    
    
    self.passwordBgV.layer.masksToBounds = YES;
    self.passwordBgV.layer.borderWidth = 1;
    self.passwordBgV.layer.borderColor =  UIColorFromRGB(0xE0E0E0).CGColor;
    self.passwordBgV.layer.cornerRadius = 40/2;
    
    self.loginBtn.layer.masksToBounds = YES;
    self.loginBtn.layer.borderWidth = 1;
    self.loginBtn.layer.borderColor =  UIColorFromRGB(0xE0E0E0).CGColor;
    self.loginBtn.layer.cornerRadius = 45/2;
}

- (IBAction)smsAction:(id)sender {
    WEAKSELF
    [[MainApi sharedInstance] postPath:@"Sms/send" params:@{@"mobile":self.accountTF.text} resultBlock:^(NSDictionary *responseObject, NSError *error) {
        STRONGSELF
    
        strongSelf.fireDate = [NSDate date];
        [strongSelf.sendTimer setFireDate:[NSDate date]];
    }];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)sendSms {
    NSDate *sDate = self.fireDate;
    NSTimeInterval t = [[NSDate date] timeIntervalSinceDate:sDate];
    NSInteger timeT = 60 - t;
    if (timeT <= 0)
    {
        [self.smsButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        [self.smsButton setUserInteractionEnabled:YES];
        [self.sendTimer setFireDate:[NSDate distantFuture]];
        
        [self.smsButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.smsButton setBackgroundColor:UIColorFromRGB(0xD8CBA5)];
        return ;
    }
    self.smsButton.userInteractionEnabled = NO;
    [self.smsButton setTitle:[NSString stringWithFormat:@"%zd秒", timeT] forState:UIControlStateNormal];
    [self.smsButton setBackgroundColor:UIColorFromRGB(0xcdd2da)];
    
    
}
- (IBAction)bindingAction:(id)sender {
    NSDictionary * params = nil;
    if (self.accountTF.text && self.passwordTF.text &&self.openid && self.platform) {
        params = @{@"mobile":self.accountTF.text,@"captcha":self.passwordTF.text,@"openid":self.openid,@"platform":self.platform};
    }else{
        return ;
    }
        
  
    [[MainApi sharedInstance] postPath:@"User/bindmobile" params:params resultBlock:^(NSDictionary *responseObject, NSError *error) {
        //        NSLog(@"%@===",responseObject);
        NSDictionary * userinfo  = responseObject[@"data"][@"userinfo"];
        [self saveUserInfo:userinfo];
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
}

- (void)saveUserInfo:(NSDictionary *)userinfo{
    
    UserInfo.avatar_url = [self filterEmpty:userinfo[@"avatar"]];
    UserInfo.userToken =   [self filterEmpty:userinfo[@"token"]];
    UserInfo.userId = [self filterEmpty:userinfo[@"user_id"]];
    UserInfo.nickname = [self filterEmpty:userinfo[@"nickname"]];
    UserInfo.mobile = [self filterEmpty:userinfo[@"mobile"]];
    UserInfo.gender =  [self filterEmpty:userinfo[@"gender"]];
    UserInfo.birthday = [self filterEmpty:userinfo[@"birthday"]];
    UserInfo.redpack_num = [self filterEmpty:userinfo[@"redpack_num"]];
    UserInfo.coupon_num =  [self filterEmpty:userinfo[@"coupon_num"]];
    UserInfo.wallet_balance = [self filterEmpty:userinfo[@"wallet_balance"]];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"USER_LOGIN_SUCCESSFUL" object:nil];
    
}
- (NSString *)filterEmpty:(id)parameter{
    NSString * str = @"";
    if (parameter &&![parameter isEqual:[NSNull null]] ) {
        str = parameter;
    }else{
        str = @"";
    }
    return str;
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.sendTimer = nil;
    self.fireDate = nil;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
